# Copyright (c) 2024 Huawei Technologies Co., Ltd.
#
# openMind is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#
#          http://license.coscl.org.cn/MulanPSL2
#
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
#!/usr/bin/bash
PY_VERSION="${PY_VERSION:-}"
pip$PY_VERSION install -e .[lint]

# Check black installation.
python$PY_VERSION -c 'import black' &> /dev/null
if [ $? -ne 0 ]; then
    echo "The 'black' python module was not found. Please try 'pip install black'."
    exit
fi

BLACK_OUTPUT=$( (black --check src tests -v) 2>&1 )
STATUS=$?
if [ "$STATUS" -ne 0 ]; then
    echo "black checks failed:"
    echo "$BLACK_OUTPUT"
    echo "Please run 'black src tests'."
    exit "$STATUS"
else
    echo "black checks passed."
fi

# Check ruff installation.
if ! hash ruff 2> /dev/null; then
    echo "The 'ruff' command was not found. Please try 'pip install ruff'."
    return
fi
RUFF_OUTPUT=$( (ruff check src tests) 2>&1 )
STATUS=$?
if [ "$STATUS" -ne 0 ]; then
    echo "ruff checks failed:"
    echo "$RUFF_OUTPUT"
    echo "Please fix ruff warning. (You could try 'ruff --fix src tests')"
    exit "$STATUS"
else
    echo "ruff checks passed."
fi
